package com.hp.bon.sgw.wh.mml.common;

import java.io.UnsupportedEncodingException;

import com.hp.bon.sgw.wh.mml.repositories.PropertyRepository;

public class MMLUtils {
	
	public static String formatLanguage(String language){
		if(language==null || language.trim().isEmpty()){
			language = "";
		}
		StringBuilder name = new StringBuilder(language.trim());
		if(name.length()==8){
			return name.toString();
		}
		int stuff = 8 - name.length() % 8;
		for (int index = 0; index < stuff; index++) {
			name.append(MMLConstants.KEY_BLANK);
		}
		return name.toString();
	}
	
	public static String formatServiceName(String serviceName){
		if(serviceName==null || serviceName.trim().isEmpty()){
			serviceName = "";
		}
		StringBuilder name = new StringBuilder(serviceName.trim());
		if(name.length()==8){
			return name.toString();
		}
		int stuff = 8 - name.length() % 8;
		for (int index = 0; index < stuff; index++) {
			name.append(MMLConstants.KEY_BLANK);
		}
		return name.toString();
	}
	
	public static String formatMMLVersion(){
		return formatMMLVersion(null);
	}
	public static String formatMMLVersion(String mmlVersion){
		String version = mmlVersion;
		if(version==null){
			version = PropertyRepository.getPropertyValue("MMLVersion");
		}
		if(version == null || version.trim().isEmpty() || version.trim().length()!=4){
			return "1.00";
		}
		return version.trim();
	}

	public static String formatPackageLength(int fixedLen, int commLen) {
		int msgLen = fixedLen + commLen;
		String msgLenStr = Integer.toHexString(msgLen);
		if (msgLenStr.length() == 4) {
			return msgLenStr;
		}

		StringBuilder sbuf = new StringBuilder();
		int stuff = 4 - msgLenStr.length() % 4;
		for (int index = 0; index < stuff; index++) {
			sbuf.insert(0,MMLConstants.KEY_ZERO);
		}
		sbuf.append(msgLenStr);

		return sbuf.toString();
	}
	
	/*
	 *  term 长度必为8, 默认值为00000000
	 */
	public static String formatTermIdentity(String term) {
		if (term == null) {
			return MMLConstants.MML_DEFAULT_TERM;
		}
		if (term.length() > 8) {
			return term.substring(0, 8);
		} else if(term.length() == 8){
			return term;
		}
		
		StringBuilder sbuf = new StringBuilder();
		int stuff = 8 - term.length() % 8;
		for (int index = 0; index < stuff; index++) {
			//sbuf.insert(0,MMLConstants.KEY_BLANK);
			sbuf.append(MMLConstants.KEY_BLANK);
		}
		sbuf.append(term);

		return term;
	}
	
	/*
	 *  session id 用16进制数表示，最大8位，不足左补空
	 */
	public static String formatSessionId(int sessionId){
		return formatId(sessionId).toUpperCase();
	}	
	/*
	 *  transaction id 用16进制数表示，最大8位，不足左补空
	 */
	public static String formatTransactionId(int transactionId){
		return formatId(transactionId);
	}
	/*
	 * id 用16进制数表示，最大8位，不足左补空
	 */
	public static String formatId(int id){
		String hexSessionId = Integer.toHexString(id);
		if(hexSessionId.length()==8){
			return hexSessionId;
		}
		
		StringBuilder sbuf = new StringBuilder();
		int stuff = 8 - hexSessionId.length() % 8;
		for(int index=0;index<stuff;index++){
			sbuf.insert(0,MMLConstants.KEY_ZERO);		
		}
		sbuf.append(hexSessionId);
		
		return sbuf.toString();
	}
	
	/*
	 * 产生消息校验码
	 */
	public static String genCheckSummary(String message) throws UnsupportedEncodingException {
		byte[] msgArray = message.getBytes("UTF-8");
		byte[] res = new byte[4];
		for (int i = 0; i < msgArray.length; i += 4) {
			res[0] ^= msgArray[i];
			res[1] ^= msgArray[i + 1];
			res[2] ^= msgArray[i + 2];
			res[3] ^= msgArray[i + 3];
		}
		res[0] = (byte) ~res[0];
		res[1] = (byte) ~res[1];
		res[2] = (byte) ~res[2];
		res[3] = (byte) ~res[3];

		StringBuilder resStr = new StringBuilder();
		for (int i = 0; i < 4; i++) {
			resStr.append(byte2hex(res[i]));
		}
		return resStr.toString();
	}
	
	/*
	 * 将单字节转成16进制
	 */
	private static String byte2hex(byte b) {
	    StringBuilder buf = new StringBuilder();
	    char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
	    int high = ((b & 0xf0) >> 4);
	    int low = (b & 0x0f);
	    buf.append(hexChars[high]);
	    buf.append(hexChars[low]);
	    return buf.toString();
	}
	
	public static void main(String[] args) throws UnsupportedEncodingException{
		System.out.println(MMLUtils.formatPackageLength(60, 10240));
		System.out.println(genCheckSummary("11102345"));
		System.out.println(MMLUtils.formatSessionId(Integer.MAX_VALUE));
		System.out.println(MMLUtils.formatSessionId(100));
		System.out.println(Integer.MAX_VALUE);
	}
}
